package com.sc.nft.config.chain;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mate.sdk.blockchain.BlockChainClient;
import com.sc.nft.util.PropertiesUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;

@Component
@Slf4j
public class BlockChainClientFactory {

    private static BlockChainClient client;

    private BlockChainClientFactory() {
        // 私有构造函数，防止外部实例化
    }

    public static BlockChainClient getBeanInstance() {
        if (ObjectUtil.isNull(client)) {
            synchronized (BlockChainClientFactory.class) {
                if (ObjectUtil.isNull(client)) {
                    log.info(StrUtil.format("链接地址:{}",ChainClientConfig.rpcAddr));
                    log.info(StrUtil.format("线程池核心线程数:{}",ChainClientConfig.nThreads));
                    client = BlockChainClient.builder()
                            .rpcAddr(ChainClientConfig.rpcAddr)
                            // 线程池有2种方式：1自定义线程池 2使用sdk线程池，但可以自定义参数
                            .executor(Executors.newFixedThreadPool(ChainClientConfig.nThreads)) // 1.自定义线程池
                            .build();
                }
            }
        }
        return client;
    }
}
